package com.xiaotu.server.controller;


import com.xiaotu.result.R;
import com.xiaotu.server.pojo.entity.IntegralGrade;
import com.xiaotu.server.service.IntegralGradeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 积分等级表 前端控制器
 * </p>
 *
 * @author xiaotu
 * @since 2022-01-07
 */
@Api("积分等级管理")
@RestController
@CrossOrigin
@RequestMapping("/server/integralGrade")
public class IntegralGradeController {
    @Autowired
    private IntegralGradeService integralGradeService;
    @ApiOperation("积分等级列表")
    @GetMapping
    public R listAll(){
        List<IntegralGrade> list =
                integralGradeService.list();
        return R.ok().data("list", list);
    }
    @ApiOperation("根据id查询积分等级")
    @GetMapping("/{id}")
    public R getById(@ApiParam(value = "数据id",required = true,example = "1")@PathVariable Long id){
        IntegralGrade integralGrade = integralGradeService.getById(id);
        if(integralGrade != null){
            return R.ok().data("record", integralGrade);
        }else{
            return R.error().message("数据不存在");
        }
    }
    @ApiOperation("新增积分等级")
    @PostMapping
    public R save(@ApiParam(value = "积分等级对象",required = true) @RequestBody IntegralGrade integralGrade){
        boolean save = integralGradeService.save(integralGrade);
        if (save){
            return R.ok().message("保存成功");
        }
        return R .error().message("保存失败");
    }
    @ApiOperation("更新积分等级")
    @PutMapping
    public R updateById(
            @ApiParam(value = "积分等级对象", required = true)
            @RequestBody IntegralGrade integralGrade){
        boolean result = integralGradeService.updateById(integralGrade);
        if(result){
            return R.ok().message("修改成功");
        }else{
            return R.error().message("修改失败");
        }
    }
    @ApiOperation(value = "根据id删除积分等级",notes = "逻辑删除")
    @DeleteMapping("/{id}")
    public R RemoveById(@ApiParam(value = "数据id",required = true,example = "1") @PathVariable Long id){
        boolean b = integralGradeService.removeById(id);
        if(b){
            return R.ok().message("删除成功");
        }
        return R.error().message("删除失败");
    }
}
